<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <!--      按姓名、医院、住院日期-->
      <el-form-item label="姓名">
        <el-input v-model="dataForm.patientName" placeholder="请输入姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="医院">
        <el-select v-model="dataForm.hospitalId" filterable placeholder="请选择医院" clearable>
          <el-option
            v-for="item in hospitalList"
            :key="item.hospitalId"
            :label="item.hospitalName"
            :value="item.hospitalId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出院时间">
        <el-date-picker
          v-model="dataForm.seTime"
          size="small"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!--        <el-button v-if="isAuth('generator:bsdepartmentpatient:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>-->
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        <el-button size="primary" @click="exportExcel()">导出</el-button>

      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="departmentPatientId"
        header-align="center"
        align="center"
        label="部门患者映射ID">
      </el-table-column>
      <el-table-column
        prop="hospitalName"
        header-align="center"
        align="center"
        label="医院">
      </el-table-column>
      <el-table-column
        prop="departmentName"
        header-align="center"
        align="center"
        label="科室">
      </el-table-column>
      <el-table-column
        prop="patientName"
        header-align="center"
        align="center"
        label="患者名称">
      </el-table-column>
      <el-table-column prop="gender" header-align="center" align="center" label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender === 1" size="small" type="success">男</el-tag>
          <el-tag v-else type="info" size="small">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="wardNumber"
        header-align="center"
        align="center"
        label="病室号">
      </el-table-column>
      <el-table-column
        prop="bedNumber"
        header-align="center"
        align="center"
        label="病床号">
      </el-table-column>
      <el-table-column
        prop="hospitalizationDate"
        header-align="center"
        align="center"
        label="住院时间">
      </el-table-column>
      <el-table-column
        prop="leaveHospitalDate"
        header-align="center"
        align="center"
        label="出院时间">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="hospitalizationPicture"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="入院照片">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="otherMessage"
        header-align="center"
        align="center"
        label="其他信息">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.departmentPatientId)">查看上报</el-button>
          <!--          <el-button type="text" size="small" @click="deleteHandle(scope.row.departmentPatientId)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
    import AddOrUpdate from './bsdepartmentpatient-add-or-update'

    export default {
        data() {
            return {
                dataForm: {
                    key: '',
                    hospitalId: '',
                    patientName: '',
                    seTime: [],
                },
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }],
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalPage: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false
            }
        },

        components: {
            AddOrUpdate
        },
        created() {
            //加载医院
            this.$http({
                url: this.$http.adornUrl(`/bs/hospital/getList`),
                method: 'get',
                params:this.$http.adornParams()
            }).then(({data}) => {
                if (data && data.code === 0) {
                    this.hospitalList = data.res;
                }
            })
        },
        activated() {
            this.getDataList()
        },
        methods: {
            exportExcel(id) {

                var startTime;
                var endTime;
                if(this.dataForm.seTime && this.dataForm.seTime.length > 1){
                    startTime = this.dataForm.seTime[0];
                    endTime = this.dataForm.seTime[1];
                }
                this.$http({
                    url: this.$http.adornUrl('/bs/departmentPatient/export'),
                    method: 'get',
                    params: this.$http.adornParams({
                        'hospitalId': this.dataForm.hospitalId,
                        'patientName': this.dataForm.patientName,
                        'leaveStartTime': startTime,
                        'leaveEndTime': endTime,
                        'isLeave': 1,
                    }),
                    responseType: 'blob'
                }).then(res => {
                    console.log("kaishidaochu")
                    console.log(res)
                    const content = res.data
                    const blob = new Blob([content], {type: "application/octet-stream"})
                    const fileName = '已出院记录.xls'
                    if ('download' in document.createElement('a')) { // 非IE下载
                        const elink = document.createElement('a')
                        elink.download = fileName
                        elink.style.display = 'none'
                        elink.href = URL.createObjectURL(blob)
                        document.body.appendChild(elink)
                        elink.click()
                        URL.revokeObjectURL(elink.href) // 释放URL 对象
                        document.body.removeChild(elink)
                    } else { // IE10+下载
                        navigator.msSaveBlob(blob, fileName)
                    }
                }).catch(err => {
                    console.log(err)
                })

            },
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true
                console.log(this.dataForm.seTime)
                this.dataListLoading = true
                var startTime;
                var endTime;
                if(this.dataForm.seTime && this.dataForm.seTime.length > 1){
                    startTime = this.dataForm.seTime[0];
                    endTime = this.dataForm.seTime[1];
                }
                this.$http({
                    url: this.$http.adornUrl('/bs/departmentPatient/list'),
                    method: 'get',
                    params: this.$http.adornParams({
                        'page': this.pageIndex,
                        'limit': this.pageSize,
                        'hospitalId': this.dataForm.hospitalId,
                        'patientName': this.dataForm.patientName,
                        'leaveStartTime': startTime,
                        'leaveEndTime': endTime,
                        'isLeave': 1,
                    })
                }).then(({data}) => {
                    if (data && data.code === 0) {
                        this.dataList = data.page.list
                        this.totalPage = data.page.totalCount
                    } else {
                        this.dataList = []
                        this.totalPage = 0
                    }
                    this.dataListLoading = false
                })
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                var ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.departmentPatientId
                })
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl('/bs/departmentPatient/delete'),
                        method: 'post',
                        data: this.$http.adornData(ids, false)
                    }).then(({data}) => {
                        if (data && data.code === 0) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList()
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                })
            }
        }
    }
</script>
